import time
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from faker import Faker
from AppTest.page.main_page import MainPage
from AppTest.page.personnelassistantpage.bfmember_page import BfMemberPage
from AppTest.page.personnelassistantpage.induction_page import InductionPage
from AppTest.page.personnelassistantpage.pmain_page import PMainPage
from AppTest.page.personnelassistantpage.roster_page import RosterPage
from AppTest.page.workbench_page import WorkbenchPage


class TestPage:

    def setup(self):
        caps = {"platformName": "Android",
                "appPackage": "com.tencent.wework",
                "appActivity": ".launch.LaunchSplashActivity",
                "deviceName": "127.0.0.1:5555",
                "noReset": True,
                "ensureWebviewsHavePages": True}

        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
        self._IMPLICI_WAIT = 10
        self.driver.implicitly_wait(self._IMPLICI_WAIT)

    def teardown(self):
        time.sleep(2)
        self.driver.quit()

    def test_pmain(self):
        mp = MainPage(self.driver)
        mp.ToWorkbench()
        wp = WorkbenchPage(self.driver)
        wp.ToPersonnelAssistant()
        pmp = PMainPage(self.driver)
        pmp.to_pending_overtime()
        pmp.to_pending_today()
        pmp.to_pending_advance()

    def test_roster_select(self):
        mp = MainPage(self.driver)
        mp.ToWorkbench()
        wp = WorkbenchPage(self.driver)
        wp.ToPersonnelAssistant()
        pmp = PMainPage(self.driver)
        pmp.to_roster()
        rp = RosterPage(self.driver)
        rp.select_all()
        rp.select_other()
        rp.select_formal()
        rp.select_fulltime()
        rp.select_internship()

    def test_roster_add(self):
        mp = MainPage(self.driver)
        mp.ToWorkbench()
        wp = WorkbenchPage(self.driver)
        wp.ToPersonnelAssistant()
        pmp = PMainPage(self.driver)
        pmp.to_roster()
        rp = RosterPage(self.driver)
        fake = Faker("zh_Cn")
        name = fake.name()
        phnum = fake.phone_number()
        rp.to_add()
        rp.add_name(name)
        rp.add_phonenumber(phnum)
        rp.add_sex("女")
        rp.add_emptype("实习")
        rp.add_date()
        rp.add_department("鹤望兰")
        rp.add_save()
        by = (AppiumBy.XPATH, "//*[contains(@text, '已添加')]")
        print(rp.do_find(by).get_attribute("text"))

    def test_roster_select(self):
        mp = MainPage(self.driver)
        mp.ToWorkbench()
        wp = WorkbenchPage(self.driver)
        wp.ToPersonnelAssistant()
        pmp = PMainPage(self.driver)
        pmp.to_roster()
        rp = RosterPage(self.driver)
        a = rp.search_input('zz')
        result = ''.join(x for x in a[0].tag_name if x.isprintable())
        assert 'zz' in result

    def test_induction(self):
        mp = MainPage(self.driver)
        mp.ToWorkbench()
        wp = WorkbenchPage(self.driver)
        wp.ToPersonnelAssistant()
        pmp = PMainPage(self.driver)
        pmp.to_manage_induction()
        ip = InductionPage(self.driver)
        ip.to_addemp()
        fake = Faker("zh_Cn")
        name = fake.name()
        phnum = fake.phone_number()
        ip.add_name(name)
        ip.add_phonenumber(phnum)
        ip.add_sex("女")
        ip.add_emptype("实习")
        ip.add_date()
        ip.add_department("鹤望兰")
        ip.add_save()

    def test_inductionsp(self):
        mp = MainPage(self.driver)
        mp.ToWorkbench()
        wp = WorkbenchPage(self.driver)
        wp.ToPersonnelAssistant()
        pmp = PMainPage(self.driver)
        pmp.to_manage_induction()
        ip = InductionPage(self.driver)
        ip.to_empdetail()
        ip.to_dinduction()
        ip.set_probation("1个月")
        ip.to_dinduction()

    def test_bfmember(self):
        mp = MainPage(self.driver)
        mp.ToWorkbench()
        wp = WorkbenchPage(self.driver)
        wp.ToPersonnelAssistant()
        pmp = PMainPage(self.driver)
        pmp.to_manage_brworker()
        bp = BfMemberPage(self.driver)
        bp.to_empdetail()
        bp.to_dinduction()
        bp.set_probation()
        bp.to_dinduction()

